<template>
  <el-form
    v-if="config.length>0"
    ref="filterForm"
    :inline="true"
    :model="filterModel"
    class="filter-container"
    size="small"
    @submit.native.prevent
  >
    <el-form-item
      v-for="(item,index) in config"
      :key="index"
      :prop="item.prop"
    >
      <!--输入框表单类型-->
      <el-input
        v-if="item.type ==='text'"
        v-model="filterData[item.prop]"
        :placeholder="item.placeholder?item.placeholder:item.label"
        @keyup.enter.native="handleFilter"
      />
      <!--checkbox表单类型-->
      <el-checkbox-group
        v-if="item.type === 'checkbox'"
        v-model="filterData[item.prop]"
        :placeholder="item.placeholder?item.placeholder:'请选择'+item.label"
      >
        <el-checkbox
          v-for="option in item.data"
          :key="option.id"
          :label="option.id"
          @change="handleFilter"
        >{{ option.name }}</el-checkbox>
      </el-checkbox-group>
      <!--下拉选择类型-->
      <el-select
        v-if="item.type === 'select'"
        v-model="filterData[item.prop]"
        :multiple="item.multiple"
        collapse-tags
        clearable
        :placeholder="item.placeholder?item.placeholder:'请选择'+item.label"
        @change="handleFilter"
      >
        <el-option
          v-for="option in item.data"
          :key="option.id"
          :label="option[item.labelKey]"
          :value="option[item.valueKey]"
        />
      </el-select>
      <!--日期时间类型-->
      <el-date-picker
        v-if="item.type === 'daterange'"
        v-model="filterData[item.prop]"
        type="daterange"
        align="right"
        unlink-panels
        :range-placeholder="item.placeholder && item.placeholder.range?item.placeholder.range:'至'"
        :start-placeholder="item.placeholder && item.placeholder.start?item.placeholder.start:'开始日期'"
        :end-placeholder="item.placeholder && item.placeholder.end?item.placeholder.end:'结束日期'"
        :picker-options="item.pickerOptions"
        @change="handleFilter"
      />
    </el-form-item>
    <el-form-item>
      <el-button
        v-waves
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >查询</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: 'FilterData',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    config: {
      type: Array,
      default() {
        return []
      }
    },
    filterData: {
      type: Object,
      default() {
        return {}
      }
    }
  },

  data() {
    return {
      filterModel: {}
    }
  },
  watch: {
    /* 实现表单数据的绑定，实时接收父组件的数据变化*/
    filterData() {
      this.filterModel = this.filterData
    }
  },
  mounted() {
    // 将组件上的属性赋值给当前组件内变量，因为props只能单向绑定,还需要监听属性值变化进行父子组件间交互
    this.filterModel = this.filterData
  },
  methods: {
    // 提交表单数据
    handleFilter() {
      this.$emit('filter', this.filterModel)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.filter-container {
  padding-bottom: 20px;
  padding: 15px;
  margin-bottom: 10px;
  background-color: #f5f7fd;
  border-radius: 5px;
  .el-input {
    width: 100% !important;
  }
  .el-select {
    width: 100% !important;
  }
  .el-form-item {
    margin-bottom: 0;
  }
}

.flex-form-item {
  display: grid;
  gap: 15px 2%;
  grid-template-columns: repeat(3, 32%);
  & + .flex-form-item {
    margin-top: 15px;
  }
}
</style>
